package com.pcbly.web.persistence.crm;
/**
* @author carl.song
* Copyright (c) 2010 Lightsoft, International.
* All rights reserved.
*
*/
import java.util.List;

import com.pcbly.web.components.utils.DateHelper;
import com.pcbly.web.components.utils.LightsoftStringUtils;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.bean.crm.FxddBean;
import com.pcbly.web.persistence.HibernateBaseDao;
import com.pcbly.web.components.hibernate.support.Page;

@SuppressWarnings("unchecked")
public class FxddHibernateDao extends HibernateBaseDao<FxddBean> implements FxddDao {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1733303906768126768L;

	protected Class<FxddBean> getEntityClass() {
		return FxddBean.class;
	}

	public FxddHibernateDao() {
		super();
	}
	@Override 
	public FxddBean saveFxddBean(FxddBean crm) throws Exception{
		if(logger.isDebugEnabled()){
			logger.debug(crm.getId());
		}
		String id=(new GUID()).toString();
		crm.setId(id);
		getHibernateTemplate().save(crm);
		return crm;
	}
	@Override
	public void removeFxddBeanByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		ids = ids.replaceAll(",", "','");
		String hsql = "update t_crm_fxdd set RECYCLED='1' where ID in( '" + ids + "')";
		getSession().createSQLQuery(hsql).executeUpdate();
		
	}
	@Override
	public void removeFxddBeanById(String id) throws Exception {
		// TODO Auto-generated method stub
		id = id.replaceAll(",", "','");
		String hsql = "update t_crm_fxdd set RECYCLED='1' where ID= '" + id + "'";
		getSession().createSQLQuery(hsql).executeUpdate();
		
	}
	
	public Page<FxddBean>  findFxddBeanByCond(FxddBean bean,
			Page<FxddBean> page, String orderByName, boolean asc)
			throws Exception{ 
			StringBuilder sb = new StringBuilder();
		    sb.append("select * from t_crm_fxdd where RECYCLED='0' ");
		    if (!(bean == null)) {
		    	if(!(bean.getStartDate()==null)){
		    		sb.append(" and SENDTIME ");
		    		sb.append(">='");
		    		sb.append(DateHelper.dateToStr(bean.getStartDate(), "yyyy-MM-dd"));
		    		sb.append("'");
		    	}
		    	if(!(bean.getEndDate()==null)){
		    		sb.append(" and SENDTIME ");
		    		sb.append("<='");
		    		sb.append(DateHelper.dateToStr(bean.getEndDate(), "yyyy-MM-dd"));
		    		sb.append("'");
		    	}
		    	if(!(LightsoftStringUtils.nullOrBlank(bean.getClientName()))){
		    		sb.append(" and CLIENTNAME ");
		    		sb.append(" like '%");
		    		sb.append(bean.getClientName());
		    		sb.append("%' ");
		    	}
		    	if(!(LightsoftStringUtils.nullOrBlank(bean.getState()))){
		    		sb.append(" and STATE ");
		    		sb.append(" like '%");
		    		sb.append(bean.getState());
		    		sb.append("%' ");
		    	}
		    	if(!(LightsoftStringUtils.nullOrBlank(bean.getWlzt()))){
		    		sb.append(" and WLZT ");
		    		sb.append(" like '%");
		    		sb.append(bean.getWlzt());
		    		sb.append("%' ");
		    	}
		    }
		    if (asc) {
				// criteria.addOrder(Order.asc(orderByName));
				sb.append(" order by  ");
				sb.append(orderByName);
				sb.append(" ");
			} else {
				// criteria.addOrder(Order.desc(orderByName));
				sb.append(" order by  ");
				sb.append(orderByName);
				sb.append(" desc ");
			}
			Object obj = getSession().createSQLQuery(
				"select count(t.ID) from (" + sb + ")t").uniqueResult();
			if (obj == null) {
				obj = "0";
			}
			
			page.setTotalCount(Integer.parseInt(String.valueOf(obj)));
			if(page.getTotalCount()>0){
				int first = page.getFirst();
				int lpage = first + page.getPageSize();
				if (lpage > page.getTotalCount()) {
					lpage = page.getTotalCount();
				}
				List<FxddBean> beans = getSession().createSQLQuery(
					sb.toString()).addEntity(FxddBean.class).list().subList(first, lpage);
				page.setResult(beans);
			}
			return page;
	}
}